package com.jams.goodox.activity.serve.express;

import android.app.AlertDialog;
import android.content.Intent;
import android.util.SparseArray;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hangzhou.jin.customview.TextMoreLayout;
import com.jams.goodox.R;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.activity.serve.ServeStopTime;
import com.jams.goodox.activity.serve.ServeSureOrderActivity;
import com.jams.goodox.constant.Constant;
import com.jams.goodox.entity.ExpressBean;
import com.jams.goodox.entity.ServeSubmitBean;
import com.jams.goodox.entity.ServicePeriod;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.ServiceService;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.widget.DateTimePickerDialog;

import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import retrofit2.Call;
import uama.hangzhou.image.photochoose.FourPicturesChoose;

/**
 * Created by jams_zhen on 2016/10/9.
 * 快递代寄
 */

public class InputExpressActivity extends BaseActivity {
    @Bind(R.id.tx_company)
    TextMoreLayout tx_company;
    @Bind(R.id.edit_repair)
    EditText editRepair;
    @Bind(R.id.order_homerepair_img1)
    ImageView orderHomerepairImg1;
    @Bind(R.id.order_homerepair_img2)
    ImageView orderHomerepairImg2;
    @Bind(R.id.order_homerepair_img3)
    ImageView orderHomerepairImg3;
    @Bind(R.id.order_homerepair_img4)
    ImageView orderHomerepairImg4;
    @Bind(R.id.tx_ok)
    TextView txOk;
    @Bind(R.id.linear_view)
    LinearLayout linear_view;

    private FourPicturesChoose choosePhotoFour;
    private ServeStopTime serveStopTime;

    private ExpressBean.DataBean expressDateBean;

    private Intent in;

    private String serviceDay = "";
    private String serviceTime = "";

    private String subId;

    private List<String> dateList;
    private List<String> timeZeroList;
    private List<String> timeNormalList;

    private DateTimePickerDialog dateTimePickerDialog;
    private String comMsg;
    private SparseArray<ImageView> viewSparseArray;

    @Override
    protected int getLayoutId() {
        return R.layout.express_add_activity;
    }

    @Override
    protected void init() {
        StyleUtil.titleBackKey(this, getIntent().getStringExtra("title"));
        subId=getIntent().getStringExtra("subId");
        viewSparseArray = new SparseArray<>();
        viewSparseArray.put(0,orderHomerepairImg1);
        viewSparseArray.put(1,orderHomerepairImg2);
        viewSparseArray.put(2,orderHomerepairImg3);
        viewSparseArray.put(3,orderHomerepairImg4);
        choosePhotoFour = new FourPicturesChoose(this,viewSparseArray);
        serveStopTime = new ServeStopTime(this, new ServeStopTime.InStopTime() {
            @Override
            public void setViewVisiable() {
                linear_view.setVisibility(View.VISIBLE);
            }
        });
        serveStopTime.init(subId+"");
    }

    @OnClick({R.id.tx_company, R.id.tx_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tx_company:
                in = new Intent(this, ExpressCompanyList.class);
                if (expressDateBean != null) {
                    in.putExtra("expressDateBean", expressDateBean);
                }
                in.putExtra("subId",subId);
                startActivityForResult(in, 123);
                break;
            case R.id.tx_ok:
                comMsg = editRepair.getText().toString().trim();
//                if (TextUtils.isEmpty(comMsg)) {
//                    ToastUtil.showShort(this, "请输入问题描述");
//                    return;
//                }
                if(expressDateBean==null){
                    ToastUtil.showShort(this, "请选择快递公司");
                    return;
                }
                getServiceDateTime();
                break;
        }
    }

    /**
     * 获取服务时间
     */
    private void getServiceDateTime() {
        enqueue(createCallService(ServiceService.class).getServicePeriod(subId), new RetrofitCallbackListener<ServicePeriod>() {
            @Override
            public void onSuccess(Call<ServicePeriod> call, ServicePeriod result) {
                if(result.getData()==null)return;
                dateList = result.getData().getDays();
                timeZeroList = result.getData().getTimeZero();
                timeNormalList = result.getData().getTimeNormal();
                if (result.getData().getIsOpenTime() != 0) {
                    goNext();
                    return;
                } else {
                    showNumPicker();
                }
            }

            @Override
            public void onError(String msg) {
                ToastUtil.show(msg);
            }
        });
    }

    /**
     * 弹出服务时间框
     */
    private void showNumPicker() {
        if (dateTimePickerDialog == null) {
            dateTimePickerDialog = new DateTimePickerDialog(this, dateList, timeZeroList, timeNormalList);
            dateTimePickerDialog.setOnDateTimeSetListener(new DateTimePickerDialog.OnDateTimeSetListener() {
                @Override
                public void OnDateTimeSet(AlertDialog dialog, String date, String hours) {
                    serviceDay = date;
                    serviceTime = hours;
                    goNext();
                    dialog.cancel();
                }
            });
        }
        dateTimePickerDialog.show();
    }

    private void goNext() {
        ServeSubmitBean serveSubmitBean=new ServeSubmitBean();
        serveSubmitBean.setType(Constant.SERVE_TYPE_EXPRESS);
        serveSubmitBean.setItemName(expressDateBean.getExpFirm());
        serveSubmitBean.setMsg(comMsg);
        serveSubmitBean.setPhotos(choosePhotoFour.getChosenImageList());
        serveSubmitBean.setServiceDay(serviceDay);
        serveSubmitBean.setServiceTime(serviceTime);
        serveSubmitBean.setSubId(subId);
        serveSubmitBean.setExpFirm(expressDateBean.getExpFirm());
        serveSubmitBean.setExpNo(expressDateBean.getExpCode());
        serveSubmitBean.setTitle(getIntent().getStringExtra("title"));

        ServeSureOrderActivity.go(this,serveSubmitBean);
//        ServeSureOrderActivity.go(this, Constant.SERVE_TYPE_EXPRESS, expressDateBean.getExpFirm(), comMsg, choosePhotoFour.getChosenImageList(), serviceDay, serviceTime, subId, "", null,expressDateBean.getExpFirm(),expressDateBean.getExpCode(), getIntent().getStringExtra("title"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 123) {
            if (resultCode == RESULT_OK && data != null) {
                expressDateBean = (ExpressBean.DataBean) data.getSerializableExtra("expressDateBean");
                if (expressDateBean != null)
                    tx_company.setRightText(expressDateBean.getExpFirm() + "");
            }
        }
        choosePhotoFour.setImageList(requestCode, resultCode, data);
    }
}
